<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class WxManagerRelController extends Controller
{
    //微信通知规则 展示
    public function index() {
        $where = [];
        if(!isAdmin()){
            $where[] = ['B.id','!=',1];
        }
        $data = DB::table('a_wx_manager_rel as A')
                  ->leftJoin('manager AS B', 'A.manager_id', '=', 'B.id')
                  ->leftJoin('a_wechat_notice AS C', 'A.wechat_notice_id', '=', 'C.id')
                  ->where($where)
                  ->select('A.id', 'B.username', 'B.realname', 'C.name')
                  ->orderBy('A.id','desc')->get();
        return view('admin.wxManagerRel.index', compact('data'));
    }

    //微信通知规则 添加
    public function add(Request $request) {
        if($request->isMethod('POST')) {
            $post_data = $request->only(['wechat_notice_id', 'manager_id']);
            try {
                //插入数据    insert 返回true 或 false
                $result = DB::table('a_wx_manager_rel')->insert($post_data);
            } catch(\Throwable $e) {
                return '2';
            }
            //由于框架自身不支持响应bool值，所以转化一种形式
            return $result ? '1' : '0';
        }
        else {
            $manager = DB::table('manager')->select('id', 'username', 'realname')->where('status', '2')->get();
            $a_wechat_notice = DB::table('a_wechat_notice')->get();
            return view('admin.wxManagerRel.add', compact('manager', 'a_wechat_notice'));
        }
    }


    //微信通知规则 删除
    public function del(Request $request) {

        $id = $request->id;

        // delete 返回受影响的行数
        $result = DB::table('a_wx_manager_rel')->where('id', $id)->delete();

        //由于框架自身不支持响应bool值，所以转化一种形式
        return $result >= 0 ? '1' : '0';
    }
}
